<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1 {
            position: absolute;
            top: 200px;
            width: 200px;
            height: 200px;
            background-color: skyblue;
        }
    </style>
</head>

<body>
    <button>按下动画运动</button>
    <div class="box1"></div>
    <script>
        var box1 = document.querySelector('.box1');
        var btn = document.querySelector('button');
        //封装动画
        function animate(obj, target) {
            clearInterval(obj.timer);
            obj.timer = setInterval(function () {
                if (obj.offsetLeft == target) {
                    clearInterval(obj.timer);
                }
                obj.style.left = box1.offsetLeft + 1 + 'px';
            }, 30)
        }

        //调用动画函数
        btn.addEventListener('click', function () {
            animate(box1, 100);
        })
    </script>
</body>

</html>